package com.itic.im.redis.template;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.itic.im.core.exception.ImException;
import com.itic.im.core.template.ImTemplate;
import com.itic.im.core.util.SoMap;
import com.itic.im.redis.RedisImManager;
import com.itic.im.redis.util.ImRedisTemplateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 消息通讯模板 redis实现
 * date: 2022/1/13 13:55
 * author: yangwl
 */
public class RedisImTemplateImpl extends ImTemplate{

    private static final Logger logger = LoggerFactory.getLogger(RedisImTemplateImpl.class);
    public static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(20);
    @Autowired
    private ImRedisTemplateUtil imRedisTemplateUtil;


    @Override
    public void send(Object o) {
        if(null == o)
            throw new ImException("data is null");
        SoMap data = (SoMap) o;
        String roomId = data.getString("roomId");
        if(StrUtil.isEmpty(roomId))
            throw new ImException("roomId can not be null");

        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                // 将消息广播到redis频道
                imRedisTemplateUtil.pub(RedisImManager.getChannel(roomId), JSONUtil.toJsonStr(data));
            }
        });
    }
}
